// AI侦探应用实体类

/**
 * 案件实体类
 */
export interface Case {
  id: number;
  title: string;
  description: string;
  status: 'pending' | 'investigating' | 'solved' | 'closed';
  priority: 'low' | 'medium' | 'high';
  createdAt: string;
  updatedAt: string;
  suspectIds: number[];
  evidenceIds: number[];
}

/**
 * 嫌疑人实体类
 */
export interface Suspect {
  id: number;
  name: string;
  age: number;
  gender: 'male' | 'female';
  occupation: string;
  description: string;
  alibi?: string;
  statements: string[];
}

/**
 * 证据实体类
 */
export interface Evidence {
  id: number;
  name: string;
  type: 'physical' | 'digital' | 'testimonial';
  description: string;
  locationFound: string;
  collectedAt: string;
  analysisResult?: string;
  relevance: 'high' | 'medium' | 'low';
}

/**
 * 调查笔记实体类
 */
export interface InvestigationNote {
  id: number;
  caseId: number;
  title: string;
  content: string;
  createdAt: string;
  updatedAt: string;
}

/**
 * JSON数据服务
 */
export class MockDataService {
  // 模拟的JSON数据存储
  private casesData: Case[] = [
    {
      id: 1,
      title: '公司机密文件失窃案',
      description: '某科技公司重要研发文件在夜间被窃取，没有明显入侵痕迹',
      status: 'investigating',
      priority: 'high',
      createdAt: '2023-10-01T08:30:00',
      updatedAt: '2023-10-05T14:20:00',
      suspectIds: [1, 2],
      evidenceIds: [1, 2, 3]
    },
    {
      id: 2,
      title: '仓库货物失踪案',
      description: '物流公司仓库一批贵重货物在周末期间神秘失踪',
      status: 'pending',
      priority: 'medium',
      createdAt: '2023-10-03T10:15:00',
      updatedAt: '2023-10-03T10:15:00',
      suspectIds: [3],
      evidenceIds: [4]
    },
    {
      id: 3,
      title: '网络诈骗案',
      description: '多名受害者报告遭遇电信网络诈骗，损失惨重',
      status: 'solved',
      priority: 'high',
      createdAt: '2023-09-15T09:45:00',
      updatedAt: '2023-10-02T16:30:00',
      suspectIds: [4, 5],
      evidenceIds: [5, 6, 7]
    }
  ];

  private suspectsData: Suspect[] = [
    {
      id: 1,
      name: '张明',
      age: 35,
      gender: 'male',
      occupation: '研发经理',
      description: '案发当晚在公司加班',
      alibi: '声称在办公室加班到晚上10点',
      statements: ['我当晚一直在处理项目文档', '我没有进入过机密文件室']
    },
    {
      id: 2,
      name: '李华',
      age: 28,
      gender: 'female',
      occupation: '数据分析师',
      description: '拥有访问机密文件的权限',
      statements: ['我在下午6点就离开了公司', '我不清楚文件是如何被盗的']
    },
    {
      id: 3,
      name: '王强',
      age: 42,
      gender: 'male',
      occupation: '仓库管理员',
      description: '负责周末值班',
      alibi: '称家中有事请假，但没有正式报备',
      statements: ['我确实在周末没有去值班', '但我绝对没有偷任何东西']
    },
    {
      id: 4,
      name: '赵丽',
      age: 32,
      gender: 'female',
      occupation: '客服代表',
      description: '通过电话接触受害者',
      statements: ['我只是按照公司流程工作', '我不知道这是诈骗活动']
    },
    {
      id: 5,
      name: '陈杰',
      age: 38,
      gender: 'male',
      occupation: '无业',
      description: '多个银行账户有异常资金流动',
      statements: ['那些钱是朋友转给我的', '我不认识什么受害者']
    }
  ];

  private evidenceData: Evidence[] = [
    {
      id: 1,
      name: '监控录像',
      type: 'digital',
      description: '显示案发时间走廊的监控画面',
      locationFound: '公司监控室',
      collectedAt: '2023-10-01T09:00:00',
      analysisResult: '发现有人在23:45分进入文件室',
      relevance: 'high'
    },
    {
      id: 2,
      name: '指纹样本',
      type: 'physical',
      description: '从文件柜上提取的指纹',
      locationFound: '机密文件室',
      collectedAt: '2023-10-01T10:30:00',
      relevance: 'medium'
    },
    {
      id: 3,
      name: '门禁记录',
      type: 'digital',
      description: '公司大楼入口和出口的门禁刷卡记录',
      locationFound: '安保部门',
      collectedAt: '2023-10-01T11:00:00',
      relevance: 'high'
    },
    {
      id: 4,
      name: '仓库监控',
      type: 'digital',
      description: '周末期间仓库的监控录像',
      locationFound: '物流中心',
      collectedAt: '2023-10-03T14:00:00',
      relevance: 'high'
    },
    {
      id: 5,
      name: '通话记录',
      type: 'digital',
      description: '诈骗电话的通话记录和录音',
      locationFound: '电信公司',
      collectedAt: '2023-09-16T10:00:00',
      relevance: 'high'
    },
    {
      id: 6,
      name: '银行转账记录',
      type: 'digital',
      description: '受害者向嫌疑人账户转账的记录',
      locationFound: '银行',
      collectedAt: '2023-09-17T15:30:00',
      relevance: 'high'
    },
    {
      id: 7,
      name: '受害者证词',
      type: 'testimonial',
      description: '多名受害者的详细陈述',
      locationFound: '公安局',
      collectedAt: '2023-09-18T09:00:00',
      relevance: 'medium'
    }
  ];

  private notesData: InvestigationNote[] = [
    {
      id: 1,
      caseId: 1,
      title: '初步调查',
      content: '文件失窃时间应该在晚上10点至凌晨2点之间，需要重点排查有文件室钥匙的人员。',
      createdAt: '2023-10-01T15:00:00',
      updatedAt: '2023-10-01T15:00:00'
    },
    {
      id: 2,
      caseId: 3,
      title: '结案报告',
      content: '根据银行转账记录和通话录音，确认赵丽和陈杰共同实施了诈骗活动。涉案金额约200万元，已追回部分赃款。',
      createdAt: '2023-10-02T14:00:00',
      updatedAt: '2023-10-02T16:30:00'
    }
  ];

  // 获取所有案件
  async getAllCases(page: number = 1, pageSize: number = 10): Promise<{ data: Case[], total: number }> {
    // 模拟网络延迟
    await new Promise(resolve => setTimeout(resolve, 300));
    
    const startIndex = (page - 1) * pageSize;
    const endIndex = startIndex + pageSize;
    
    return {
      data: this.casesData.slice(startIndex, endIndex),
      total: this.casesData.length
    };
  }

  // 根据ID获取案件详情
  async getCaseById(id: number): Promise<Case | null> {
    await new Promise(resolve => setTimeout(resolve, 200));
    return this.casesData.find(c => c.id === id) || null;
  }

  // 根据案件ID获取嫌疑人
  async getSuspectsByCaseId(caseId: number): Promise<Suspect[]> {
    await new Promise(resolve => setTimeout(resolve, 200));
    const caseData = this.casesData.find(c => c.id === caseId);
    if (!caseData) return [];
    
    return this.suspectsData.filter(s => caseData!.suspectIds.includes(s.id));
  }

  // 根据案件ID获取证据
  async getEvidenceByCaseId(caseId: number): Promise<Evidence[]> {
    await new Promise(resolve => setTimeout(resolve, 200));
    const caseData = this.casesData.find(c => c.id === caseId);
    if (!caseData) return [];
    
    return this.evidenceData.filter(e => caseData!.evidenceIds.includes(e.id));
  }

  // 获取案件的调查笔记
  async getNotesByCaseId(caseId: number): Promise<InvestigationNote[]> {
    await new Promise(resolve => setTimeout(resolve, 200));
    return this.notesData.filter(n => n.caseId === caseId);
  }

  // 添加新案件
  async addCase(newCase: Omit<Case, 'id' | 'createdAt' | 'updatedAt'>): Promise<Case> {
    await new Promise(resolve => setTimeout(resolve, 300));
    
    const now = new Date().toISOString();
    const caseWithId: Case = {
      ...newCase,
      id: this.casesData.length + 1,
      createdAt: now,
      updatedAt: now
    };
    
    this.casesData.push(caseWithId);
    return caseWithId;
  }

  // 更新案件
  async updateCase(id: number, updates: Partial<Case>): Promise<Case | null> {
    await new Promise(resolve => setTimeout(resolve, 300));
    
    const index = this.casesData.findIndex(c => c.id === id);
    if (index === -1) return null;
    
    this.casesData[index] = {
      ...this.casesData[index],
      ...updates,
      updatedAt: new Date().toISOString()
    };
    
    return this.casesData[index];
  }

  // 过滤案件
  async filterCases(filters: Partial<Case>, page: number = 1, pageSize: number = 10): Promise<{ data: Case[], total: number }> {
    await new Promise(resolve => setTimeout(resolve, 300));
    
    let filteredData = [...this.casesData];
    
    // 应用过滤条件
    if (filters.title) {
      filteredData = filteredData.filter(c => c.title.includes(filters.title!));
    }
    if (filters.status) {
      filteredData = filteredData.filter(c => c.status === filters.status);
    }
    if (filters.priority) {
      filteredData = filteredData.filter(c => c.priority === filters.priority);
    }
    
    const startIndex = (page - 1) * pageSize;
    const endIndex = startIndex + pageSize;
    
    return {
      data: filteredData.slice(startIndex, endIndex),
      total: filteredData.length
    };
  }

  // 删除案件
  async deleteCase(id: number): Promise<boolean> {
    await new Promise(resolve => setTimeout(resolve, 300));
    
    const index = this.casesData.findIndex(c => c.id === id);
    if (index === -1) return false;
    
    this.casesData.splice(index, 1);
    return true;
  }

  // 添加嫌疑人
  async addSuspect(newSuspect: Omit<Suspect, 'id'>): Promise<Suspect> {
    await new Promise(resolve => setTimeout(resolve, 200));
    
    const suspectWithId: Suspect = {
      ...newSuspect,
      id: this.suspectsData.length + 1
    };
    
    this.suspectsData.push(suspectWithId);
    return suspectWithId;
  }

  // 添加证据
  async addEvidence(newEvidence: Omit<Evidence, 'id'>): Promise<Evidence> {
    await new Promise(resolve => setTimeout(resolve, 200));
    
    const evidenceWithId: Evidence = {
      ...newEvidence,
      id: this.evidenceData.length + 1
    };
    
    this.evidenceData.push(evidenceWithId);
    return evidenceWithId;
  }

  // 添加调查笔记
  async addInvestigationNote(newNote: Omit<InvestigationNote, 'id' | 'createdAt' | 'updatedAt'>): Promise<InvestigationNote> {
    await new Promise(resolve => setTimeout(resolve, 200));
    
    const now = new Date().toISOString();
    const noteWithId: InvestigationNote = {
      ...newNote,
      id: this.notesData.length + 1,
      createdAt: now,
      updatedAt: now
    };
    
    this.notesData.push(noteWithId);
    return noteWithId;
  }

  // 为案件添加嫌疑人
  async addSuspectToCase(caseId: number, suspectId: number): Promise<boolean> {
    await new Promise(resolve => setTimeout(resolve, 200));
    
    const caseData = this.casesData.find(c => c.id === caseId);
    const suspectExists = this.suspectsData.some(s => s.id === suspectId);
    
    if (!caseData || !suspectExists || caseData.suspectIds.includes(suspectId)) {
      return false;
    }
    
    caseData.suspectIds.push(suspectId);
    caseData.updatedAt = new Date().toISOString();
    return true;
  }

  // 为案件添加证据
  async addEvidenceToCase(caseId: number, evidenceId: number): Promise<boolean> {
    await new Promise(resolve => setTimeout(resolve, 200));
    
    const caseData = this.casesData.find(c => c.id === caseId);
    const evidenceExists = this.evidenceData.some(e => e.id === evidenceId);
    
    if (!caseData || !evidenceExists || caseData.evidenceIds.includes(evidenceId)) {
      return false;
    }
    
    caseData.evidenceIds.push(evidenceId);
    caseData.updatedAt = new Date().toISOString();
    return true;
  }
}